<%--
  Created by IntelliJ IDEA.
  User: teacher
  Date: 2022/12/15
  Time: 11:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
    <script>
        function checkUsername(){
            let xhr = new XMLHttpRequest();
            xhr.onreadystatechange=function(){
                if(xhr.status == 200 && xhr.readyState == 4){
                    let result = xhr.responseText;
                    document.getElementById("s1").innerHTML = result;
                }
            };
            let username = document.getElementById("username").value;
            <%--xhr.open("get","${pageContext.request.contextPath}/checkUsername?username="+username,true);--%>
            <%--xhr.send(null);--%>

            // post请求
            // 在ajax中,请求参数默认是以payload的方式进行传输的,而并不是表单数据
            // 查看请求头中Content-Type,发现其值是text/plain
            // 因此,必须更改数据传输方式
            // 设置请求头
            xhr.open("post","${pageContext.request.contextPath}/checkUsername",true);
            // 必须在有请求之后才能设置
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
            xhr.send("username="+username);
        }
    </script>
</head>
<body>
<h1>用户注册</h1>
<a href="${pageContext.request.contextPath}/jsp/login.jsp">返回登录</a>
<hr>
<form action="${pageContext.request.contextPath}/register.user" method="post">
    <!--
        当用户输入用户名之后立刻判断,当前用户名是否可用
    -->
    用户名:<input type="text" name="username" id="username" onblur="checkUsername()"><span id="s1"></span><br>
    密&nbsp;&nbsp;&nbsp;码:<input type="password" name="password"><br>
    电&nbsp;&nbsp;&nbsp;话:<input type="text" name="phone"><br>
    地&nbsp;&nbsp;&nbsp;址:<input type="text" name="address"><br>
    <input type="submit" value="注册">
    <span style="color:red;">${registerMsg}</span>
</form>
</body>
</html>